/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ocr.handler;

import cn.ac.iie.di.ocr.util.FtpTools;
import cn.ac.iie.di.ocr.util.GlobalParameters;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.log4j.Logger;
import redis.clients.jedis.Jedis;

/**
 *
 * @author Administrator
 */
public class OCRThreadPoolTask implements Runnable {

    static private final Logger logger = Logger.getLogger(OCRThreadPoolTask.class.getName());
    private Message list;
    private FTPClient createFtpClient = null;
    private FTPClient deleteFtpClient = null;
    private String ftpip = "";
    private String ftppath = "";
    private Integer ftpport = -1;
    private String username = "";
    private String passwd = "";

    public OCRThreadPoolTask(Message list) {
        this.list = list;
    }

    @Override
    public void run() {
        try {
            logger.info("get " + list.getData().size() + " datas form picHandlerQueue!");
            List names = Arrays.asList(list.getColNames());
//            int mreca = names.indexOf("mreca");
            int type = names.indexOf("type");
            int cont = names.indexOf("cont");
            int uwxid = names.indexOf("uwxid");
            int usip = names.indexOf("usip");
            int ulc = names.indexOf("ulc");
            int ulp = names.indexOf("ulp");
            int mmid = names.indexOf("mmid");
            int mmf = names.indexOf("mmf");
            int mdf = names.indexOf("mdf");
            int mccode = names.indexOf("mccode");
            int murl = names.indexOf("murl");
            int uchid = names.indexOf("uchid");
            int mcr = names.indexOf("mcr");
            int mid = names.indexOf("mid");
            String[] colNames = list.getColNames();
            for (int i = 0; i < colNames.length; i++) {
                if ("type".equals(colNames[i])) {
                    type = i;
                }
                if ("cont".equals(colNames[i])) {
                    cont = i;
                }
                if ("uwxid".equals(colNames[i])) {
                    uwxid = i;
                }
                if ("usip".equals(colNames[i])) {
                    usip = i;
                }
                if ("ulc".equals(colNames[i])) {
                    ulc = i;
                }
                if ("ulp".equals(colNames[i])) {
                    ulp = i;
                }
                if ("mmid".equals(colNames[i])) {
                    mmid = i;
                }
                if ("mmf".equals(colNames[i])) {
                    mmf = i;
                }
                if ("mdf".equals(colNames[i])) {
                    mdf = i;
                }
                if ("mccode".equals(colNames[i])) {
                    mccode = i;
                }
                if ("murl".equals(colNames[i])) {
                    murl = i;
                }
                if ("uchid".equals(colNames[i])) {
                    uchid = i;
                }
                if ("mcr".equals(colNames[i])) {
                    mcr = i;
                }
                if ("mid".equals(colNames[i])) {
                    mid = i;
                }
            }
            logger.info("colNames " + names.size());
            String[] lists = new String[]{"mreca", "type", "cont", "uwxid", "usip", "ulc", "ulp", "mmid", "mmf", "mdf", "mccode", "murl"};
            for (Object[] msg : list.getData()) {
                Map m = new HashMap();
                m.put("mreca", 1 + "");
                m.put("type", msg[type].toString());
                m.put("cont", msg[cont].toString());
                m.put("uwxid", msg[uwxid].toString());
                m.put("usip", msg[usip].toString());
                m.put("ulc", msg[ulc].toString());
                m.put("ulp", msg[ulp].toString());
                m.put("mmid", msg[mmid].toString());
                List list = new ArrayList();
                list.add(msg[mmf].toString());
                m.put("mmf", list);
                m.put("mdf", msg[mdf].toString());
                m.put("mccode", msg[mccode].toString());
                m.put("murl", msg[murl].toString());
                m.put("uchid", msg[uchid].toString());
                m.put("mcr", msg[mcr].toString());
                m.put("mid", msg[mid].toString());
                GlobalParameters.mppQueue.put(m);
                logger.info("mppQueue " + m.toString() + " size " + GlobalParameters.mppQueue.size());
            }

        } catch (Exception e) {
            logger.error("Set redis error :" + e + list.toString(), e);
        } finally {
//            if (jedis != null) {
//                GlobalParameters.connectionHandler.returnResource(jedis);
//            }
        }

    }

}
